package com.jsz.peini.common.init;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.jsz.peini.bean.codes.SystemCity;
import com.jsz.peini.bean.codes.SystemCode;
import com.jsz.peini.bean.codes.SystemDistrict;
import com.jsz.peini.common.util.CommonUtils;
import com.jsz.peini.common.util.EhcacheUtil;
import com.jsz.peini.common.util.FileUtils;
import com.jsz.peini.mapper.sign.SignInfoMapper;
import com.jsz.peini.mapper.task.TaskInfoMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.service.codes.SystemCityService;
import com.jsz.peini.service.codes.SystemCodeService;
import com.jsz.peini.service.codes.SystemDistrictService;
import com.jsz.peini.service.seller.SellerInfoService;

public class InitData implements InitializingBean{
	Logger logger = LoggerFactory.getLogger(InitData.class);
	@Autowired
	private SystemCodeService systemCodeService;
	
	@Autowired
	private SystemCityService systemCityService;
	
	@Autowired
	private SystemDistrictService systemDistrictService;
	
	@Autowired
	private SellerInfoService sellerInfoService;
	@Resource
	private UserInfoMapper userInfoMapper;
	@Resource
	private TaskInfoMapper taskInfoMapper;
	@Resource
	private SignInfoMapper signInfoMapper;
	
	@Override
    public void afterPropertiesSet() throws Exception {
		
		System.setProperty(net.sf.ehcache.CacheManager.ENABLE_SHUTDOWN_HOOK_PROPERTY,"true");
		
		//查询字典表放入缓存
		List<SystemCode> codesList = systemCodeService.findAll();
		EhcacheUtil.getInstance().put("codeCache", "codeList", codesList);
		
		//查询省地市字典表
		List<SystemCity> provinceList = systemCityService.findAllByOrder(1);
		List<SystemCity> cityList = systemCityService.findAllByOrder(2);
		List<SystemCity> countyList = systemCityService.findAllByOrder(3);
		ArrayList<LinkedHashMap<String,Object>>  areaList = new ArrayList<LinkedHashMap<String,Object>>();
		for(SystemCity province: provinceList){
			LinkedHashMap<String,Object> pmap = new LinkedHashMap<String,Object>();
			pmap.put("id", province.getId());
			pmap.put("provinceId", province.getCodesProvince());
			pmap.put("provinceName", province.getCodesName());
			ArrayList<LinkedHashMap<String,Object>>  cList = new ArrayList<LinkedHashMap<String,Object>>();
			//循环市
			for(SystemCity city : cityList){
				LinkedHashMap<String,Object> cmap = new LinkedHashMap<String,Object>();
				if(province.getCodesProvince().equals(city.getCodesProvince())){
					cmap.put("id", city.getId());
					cmap.put("cityId", city.getCodesCity());
					cmap.put("cityName", city.getCodesName());
					ArrayList<LinkedHashMap<String,Object>>  tList = new ArrayList<LinkedHashMap<String,Object>>();
					//循环区(县)
					for(SystemCity county : countyList){
						LinkedHashMap<String,Object> tmap = new LinkedHashMap<String,Object>();
						if(city.getCodesCity().equals(county.getCodesCity())){
							tmap.put("id", county.getId());
							tmap.put("countyId", county.getCodesCounty());
							tmap.put("countyName", county.getCodesName());
							tList.add(tmap);
						}
						
					}
					cmap.put("cityObject", tList);
					cList.add(cmap);
				}
			}
			pmap.put("provinceObject", cList);
			areaList.add(pmap);
		}
		EhcacheUtil.getInstance().put("codeCache", "areaList", areaList);
		
		//查询商区字典表
		List<SystemDistrict> oneList = systemDistrictService.findCounty();
		List<SystemDistrict> twoList = systemDistrictService.findDistrict();
		ArrayList<LinkedHashMap<String,Object>>  districtList = new ArrayList<LinkedHashMap<String,Object>>();
		for(SystemDistrict dList : oneList){
			LinkedHashMap<String,Object> ddmap = new LinkedHashMap<String,Object>();
			ddmap.put("countyId", dList.getDistrictCode());
			ddmap.put("countyName", dList.getDistrictName());
			ArrayList<LinkedHashMap<String,Object>>  ccList = new ArrayList<LinkedHashMap<String,Object>>();
			
			LinkedHashMap<String,Object> ccmap = new LinkedHashMap<String,Object>();
			
			ccmap.put("districtId", dList.getDistrictCode());
			ccmap.put("districtName", "全部");
			ccList.add(ccmap);
			
			for(SystemDistrict dddList : twoList){
				
				if(dList.getDistrictCode().equals(dddList.getDistrictCode())){
					ccmap = new LinkedHashMap<String,Object>();
					//ccmap.put("districtId", dddList.getPlaceCode());
					ccmap.put("districtId", dddList.getId());
					ccmap.put("districtName", dddList.getPlcaeName());
					ccList.add(ccmap);
				}
				
			}
			ddmap.put("districtObject", ccList);
			districtList.add(ddmap);
		}
		EhcacheUtil.getInstance().put("codeCache", "districtList", districtList);
		
		//查询业态字典表
		List<LinkedHashMap<String,Object>> ytList= sellerInfoService.getSellerCodesBySellerCodes();
		EhcacheUtil.getInstance().put("codeCache", "ytList", ytList);
		
		FileUtils.fileUrl = new CommonUtils().getValue("fileUrl");
//		logger.info("初始化图片上传路径..............:"+FileUtils.fileUrl);
    }

}
